import React, { useContext, useEffect, useState } from 'react'
let Context = React.createContext()
// 上下文 + 高阶组件

export function Provider(props) {
  let { store, children } = props;
  return <Context.Provider value={{ store }}>
    {children}
  </Context.Provider>
}

export function connect(mapStateToProps, mapDispatchToProps) {
  return function (Com) {
    return function LastCom() {
      // state dispatch
      let context = useContext(Context);
      let { store } = context
      let [qqq, setQqq] = useState({})
      // context:{store:store}
      useEffect(() => {
        store.subscribe(() => {
          // console.log("用来更新组件")
          setQqq({})
        })
      }, [])
      return <Com
        qqq={123}
        {...mapStateToProps(store.getState())}
        {...mapDispatchToProps(store.dispatch)} />
    }
  }
}